package service;

import org.springframework.util.CollectionUtils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by fanzhenyu02 on 2020/4/22.
 */
public class FileProcess {


    public static void process(String fileName) throws IOException {
        FileInputStream inputStreamAll = new FileInputStream("/Users/zhfan/Downloads/web_source.csv");
        BufferedReader bufferedReaderAll = new BufferedReader(new InputStreamReader(inputStreamAll));

        String allLine = null;

        Set<String> allVidSet = new HashSet<>();
        Map<String, Integer> mkk_sourceMap = new HashMap<>();
        Map<String, Integer> jeyun_sourceMap = new HashMap<>();
        Map<String, Integer> sylee_sourceMap = new HashMap<>();
        int totalCnt = 0;

        while ((allLine = bufferedReaderAll.readLine()) != null) {
            final String[] splitArray = allLine.split(",");
            String vid = splitArray[0];
            if (!isNumeric(vid)) continue;
            allVidSet.add(vid);

            String name = splitArray[5];
            if (name.equals("mkk513")) {
                mkk_sourceMap.put(vid, mkk_sourceMap.getOrDefault(vid, 0) + 1);
                totalCnt++;
            }
            if (name.equals("jeyun01")) {
                jeyun_sourceMap.put(vid, mkk_sourceMap.getOrDefault(vid, 0) + 1);
                totalCnt++;
            }
            if (name.equals("sylee6968")) {
                sylee_sourceMap.put(vid, mkk_sourceMap.getOrDefault(vid, 0) + 1);
                totalCnt++;
            }
        }

        //close
        inputStreamAll.close();
        bufferedReaderAll.close();


        FileInputStream inputStream = new FileInputStream(fileName);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String line = null;
        int lineCount = 0, viCount = 0;
        Set<String> mkk513_viSet = new HashSet<>();
        Map<String, Integer> mkk513_viMap = new HashMap();

        while ((line = bufferedReader.readLine()) != null) {
            final String[] splitArray = line.split("\\s+");
            for (String part : splitArray) {
                if (isNumeric(part) && allVidSet.contains(part)) {
                    mkk513_viSet.add(part);
                    mkk513_viMap.put(part, mkk513_viMap.getOrDefault(part, 0) + 1);
                    viCount++;
                }
            }
        }

        for (String vid : mkk513_viMap.keySet()) {
            if (mkk513_viMap.get(vid).intValue() > 1) {
                System.out.println("mkk513 repat vi: " + vid + " and count: " + mkk513_viMap.get(vid));
            }
        }

        //close
        inputStream.close();
        bufferedReader.close();

        for (String vi : mkk513_viSet) {
            if (!mkk_sourceMap.containsKey(vi)) System.out.println(vi);
        }

//        int cnt = 0;
//        for (String vi : mkk_sourceMap.keySet()) {
//            if (!mkk513_viSet.contains(vi)) {
//                System.out.println("source not in: " + vi);
//                ++cnt;
//            }
//        }
//
//        System.out.println(cnt);
    }

    public static boolean isNumeric(String str) {
        String bigStr;
        try {
            bigStr = new BigDecimal(str).toString();
        } catch (Exception e) {
            return false;//异常 说明包含非数字。
        }
        return true;
    }

    public static void main(String[] args) throws Exception {
//        String wholeName = "/Users/zhfan/Downloads/webData.txt";
//        process(wholeName);


        List<String> productAuditList = Arrays.asList("1", "1", "2", "2", "3");
        //filter out all duplicate products by its id
        final Set<String> vendorItemIdSet = new HashSet<>();
        final List<String> filteredProductAuditList = productAuditList.stream()
                .filter(e -> vendorItemIdSet.add(e))
                .collect(Collectors.toList());


        System.out.println(filteredProductAuditList);

//        String[] appNameList = {"wxa552e31d6839de85", "wx969035790e9e1e62", "wxc72f01f43da0083b", "wx8e251222d6836a60", "weixin", "dianping-weixin", "group", "waimai"};
//        String[] subMerchantNoList = {"11000017929288"};
//        String[] payTypeList = {"36"};
//        StringBuilder content = new StringBuilder();
//        content.append("[\n");
//        for (String sub : subMerchantNoList) {
//            for (String appName : appNameList) {
//                for (String payType : payTypeList) {
//                    content.append("\n{\n" +
//                            "        \"pay_type\": \"")
//                            .append(payType).append("\",\n" +
//                            "        \"app_name\": \"")
//                            .append(appName).append("\",\n" +
//                            "        \"biz_merch_no\": \"").append(sub).append("\"\n" +
//                            "    },");
//
//                }
//            }
//        }
//
//        content.deleteCharAt(content.length() - 1);
//        content.append("\n]");
//
//        BufferedWriter out = null;
//        out = new BufferedWriter(new OutputStreamWriter(
//                new FileOutputStream(file, false)));
//        out.write(content.toString());
//        out.flush();
//        out.close();
    }


}
